<?php
namespace app\admin\model;

use think\Model;
use think\Cache;

class Auth extends Model
{

    /**
     * 查询所有数据  并且缓存
     * */
    public  function authData(){
        $res = $this->order('listorder asc')->select();
        // 格式化
        foreach($res as $k=>$v){
            $result[$v['id']] = $v;
        }
        Cache::set('auth',$result);
        return $result;
    }
    /**
     * 如果缓存数据不存在，则重新生成
     * */
    public function authCache(){
        $res = Cache::get('auth');
        if(!$res){
            $this->authData();
        }
        return $res;
    }

    /**
     * 获取单条数据
     * */
    public function authInfo($id){
        $data = $this->authCache();
        if(array_key_exists($id,$data)){
            $result = $data[$id];
        }else{
            $result = '';
        }
        return $result;
    }

    /**
     * 新增数据
     * */
    public function authSave($data){
        $res = $this->save($data);
        $this->authData();    // 更新缓存
        return $res;
    }

    /**
     * 更新数据
     * */
    public function authUpdate($data,$id){
        $res = $this->save($data,['id'=>$id]);
        $this->authData();    // 更新缓存
        return $res;
    }

    /**
     * 删除
     * */
    public function authDelete($id){
        $res =$this->destroy($id);
        return $res;
    }

    /**
     * 查询节点ID
     * @param $role_name 如：'admin/index/index'
     * @return 节点的id
     * */
    public function roleReturnID($role_name){
        $where['name'] = $role_name;

        $res = $this->where($where)->select();
        $count = count($res);

        // 查询到多个值或者一个都没找到，就返回为空
        if($count !=1){
            return false;
        }else{
            return $res[0]['id'];
        }

    }




    /**
     * 子类数组
     * */
    public function pidTree($pid=0){
        $result = '';
        $arr = $this->authCache();
        if(empty($arr)) return false;
        foreach($arr as $k=>$v){
            if($v['pid'] ==$pid){
                $result[$v['id']] = $v;
            }
        }
        return $result;
    }

    /**
     * 无限循环数据组
     * */
    public function tree($pid=0){

        $arr = $this->pidTree($pid);
        if(empty($arr)) return false;
        foreach($arr as $k=>$v){
            $result[$v['id']] = $v;

            $res = $this->tree($v['id']);
            if($res){
               foreach($res as $kk=>$vv){
                   $result[$vv['id']]['id'] = $vv['id'];
                   $result[$vv['id']]['pid'] = $vv['pid'];
                   $result[$vv['id']]['title'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|- ".$vv['title'];
                   $result[$vv['id']]['name'] = $vv['name'];
                   $result[$vv['id']]['listorder'] = $vv['listorder'];
               }
           }
        }
        return $result;
    }

    /**
     * 转opt格式
     * */
    public function authOpt($val=null){
        $opt = '';
        $data = $this->tree();
        foreach($data as $k=>$v){
            if($v['id'] == $val){
                $opt .="<option value=$v[id] selected>$v[title]</option>";
            }else{
                $opt .="<option value=$v[id]>$v[title]</option>";
            }

        }
        return $opt;
    }

    /**
     * 把分类变成结构树
     * @param array $items 数组
     * @return  结构树
     * */
    public function getTree3($items,$pid ="pid") {

        $map  = [];
        $tree = [];
        foreach ($items as &$it){ $map[$it['id']] = &$it; }  //数据的ID名生成新的引用索引树
        foreach ($items as &$it){
            $parent = &$map[$it[$pid]];
            if($parent) {
                $parent['son'][] = &$it;
            }else{
                $tree[] = &$it;
            }
        }
        return $tree;
    }
    /**
     * 无限分类 树形结构
     * */
    public function getTree($pid=0){
        $arr = $this->pidTree($pid);
        if(empty($arr)) return false;
        foreach($arr as $k=>$v){
            /*$result[$v['id']] = $v;
            $result[$v['id']]['sub'] = $this->getTree($v['id']);*/
            $result[$k] = $v;
            $result[$k]['sub'] = $this->getTree($v['id']);

        }
        return $result;

    }








}